package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.Result;
import com.itheima.ydd.dto.SysMenuDto;
import com.itheima.ydd.dto.SysRoleDto;
import com.itheima.ydd.entity.SysMenu;
import com.itheima.ydd.entity.SysRole;
import com.itheima.ydd.mapper.SysMenuMapper;
import com.itheima.ydd.service.SysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/sys_role")
@Slf4j
public class SysRoleController {
    @Autowired
    SysRoleService sysRoleService;
    @Autowired
    private SysMenuMapper menuMapper;


    //角色分页
    @GetMapping("/list")
    public Result page(String name, int page, int limit) {
        //分页构造器
        Page<SysRole> pageInfo = new Page<>(page, limit);
        //条件构造器
        LambdaQueryWrapper<SysRole> lqw = new LambdaQueryWrapper<>();
        //模糊查询
        lqw.like(name != null, SysRole::getName, name);
        //排序
        lqw.orderByDesc(SysRole::getUpdate_date);
        //调用service
        sysRoleService.page(pageInfo, lqw);

        return pageInfo != null ? Result.ok("", PageResult.answer(pageInfo.getTotal(), pageInfo.getSize(), pageInfo.getCurrent(), pageInfo.getRecords())) : Result.error("暂无标签数据");

    }

    //角色详情
    @GetMapping("/rowInfo")
    public Result selectById(String id) {
        log.info("角色详情 id:{}", id);
        SysRole sysRole = sysRoleService.getById(id);
        SysRoleDto roleDto = new SysRoleDto();
        BeanUtils.copyProperties(sysRole,roleDto);

        //查询根级菜单
        List<SysMenu> menuList = menuMapper.selectMenu(id,0);

        //查询菜单
        List<SysMenuDto> sysMenuDtos = menuList.stream().map(menu -> {
            SysMenuDto menuDto = new SysMenuDto();
            BeanUtils.copyProperties(menu, menuDto);
            this.getMenuDto(id, menuDto);
            return menuDto;
        }).collect(Collectors.toList());
        roleDto.setAuthList(sysMenuDtos);
        return Result.ok("", roleDto);
    }

    /**
     * 递归查询
     * @param roleId
     * @param menuDto
     */
    private void getMenuDto(String roleId, SysMenuDto menuDto) {
        Integer id = menuDto.getId();
        List<SysMenu> sysMenus = menuMapper.selectMenu(roleId, id);
        if (sysMenus.isEmpty()) {
            return;
        }
        List<SysMenuDto> sysMenuDtos = sysMenus.stream().map(menu -> {
            SysMenuDto dto = new SysMenuDto();
            BeanUtils.copyProperties(menu, dto);

            getMenuDto(roleId,dto);
            return dto;
        }).collect(Collectors.toList());
        menuDto.setChildren(sysMenuDtos);
    }

    //角色修改
    @PostMapping("/update")
    public Result update(@RequestBody SysRole sysRole) {
        log.info("角色修改 advimg:{}", sysRole);
        sysRoleService.updateById(sysRole);
        return Result.ok("", sysRole);
    }

    //角色删除
    @GetMapping("/del")
    public Result delete(String id) {
        log.info("角色删除 id:{}", id);
        sysRoleService.removeById(id);
        return Result.ok("", id);
    }

    //角色添加
    @PostMapping("/add")
    public Result add(@RequestBody SysRole sysRole) {
        log.info("角色添加 advimg:{}", sysRole);
        sysRoleService.save(sysRole);
        return Result.ok("", sysRole);
    }

    //角色全部
    @GetMapping("/allData")
    public Result all() {
        log.info("查询角色全部");
        List<SysRole> list = sysRoleService.list();
        return Result.ok("", list);
    }


}
